﻿//Hai hàm liên qua CheckAlls và CheckSingles 

function CheckAlls(idCheckAll, nameCheckSingle, formId) {
    if (document.getElementById(idCheckAll).checked == true) {
        for (var i = 0; i < document.forms[formId].length; i++) {
            var item = document.forms[formId].elements[i];
            if (item.name == nameCheckSingle && item.type == 'checkbox') {
                item.checked = true;
            }
        }
    }
    else {
        for (var i1 = 0; i1 < document.forms[formId].length; i1++) {
            var item1 = document.forms[formId].elements[i1];
            if (item1.name == nameCheckSingle && item1.type == 'checkbox') {
                item1.checked = false;
            }
        }
    }
}

function CheckSingles(idCheckAll, nameCheckSingle, formId) {

    var isCheckAll = true;

    for (var i = 0; i < document.forms[formId].length; i++) {
        var item = document.forms[formId].elements[i];
        if (item.name == nameCheckSingle && item.type == 'checkbox' && item.checked == false) {
            isCheckAll = false;
            break;
        }
    }

    if (isCheckAll) {
        document.getElementById(idCheckAll).checked = true;
    } else {
        document.getElementById(idCheckAll).checked = false;
    }
}

//end


function Today() {
    var ngay, thang, nam;
    var thu;
    var full;
    var d = new Date();
    //ngày tháng nam
    ngay = d.getDate();
    thang = d.getMonth();
    thang++;
    nam = d.getFullYear();
    //Thứ
    var days = new Array("", "Chủ nhật", "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy");
    var wday = days[d.getDay() + 1];
    if (ngay < 10)
        ngay = '0' + new String(ngay);
    if (thang < 10)
        thang = '0' + new String(thang);
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();
    var dn = "AM";
    if (hours >= 12) {
        dn = "PM";
        hours = hours;
        //this is so the hours written out is 
        //in 12-hour format, instead of the default //24-hour format.
    }
    //this is so the hours written out 
    //when hours=0 (meaning 12a.m) is 12
    if (minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds <= 9) {
        seconds = "0" + seconds;
    }
    full = hours + ":" + minutes + ":" + seconds + " " + dn + " | ";
    $("<span id='lblToday'>" + full + " " + wday + ', ' + ngay + '-' + thang + '-' + nam + "</span>")
		.replaceAll("#lblToday");
    setTimeout("Today()", 1000);
}


function ConvertDateToString(date) {
    var okDate = /Date\(([-+]?\d+[-+]?\d+)\)/ .exec(date);

    if (okDate) {
        var when = new Date(eval(okDate[1]));
        //da = new Date(date);  	// Create a Date Object set to the last modifed date
        var da = when;
        var dy = da.getFullYear();  	// Get full year (as opposed to last two digits only)
        var dm = da.getMonth() + 1;  	// Get month and correct it (getMonth() returns 0 to 11)
        var dd = da.getDate();  	// Get date within month
        if (dy < 1970) dy = dy + 100; 	// We still have to fix the millennium bug
        var ys = new String(dy); 	// Convert year, month and date to strings
        var ms = new String(dm);
        var ds = new String(dd);
        if (ms.length == 1) ms = "0" + ms; 	// Add leading zeros to month and date if required
        if (ds.length == 1) ds = "0" + ds;
        ys = ms + "/" + ds + "/" + ys;  	// Combine year, month and date in ISO format
        return ys;
    }
}

function Trim(sString) {
    while (sString.substring(0, 1) == " ") {
        sString = sString.substring(1, sString.length);
    }

    while (sString.substring(sString.length - 1, sString.length) == " ") {
        sString = sString.substring(0, sString.length - 1);
    }

    return sString;
}

function IsvalidDate(dateString, formatString) {
    if (dateString.indexOf("/") == -1) {
        return false;
    }
    var dt1 = dateString.split("/");
    var mm1 = parseInt(dt1[0]);
    var dd1 = parseInt(dt1[1]);
    var yy1 = parseInt(dt1[2]);
    if (isNaN(dd1) || isNaN(mm1) || isNaN(yy1)) {
        return false;
    }
    var dt2 = new Date(mm1 + '/' + dd1 + '/' + yy1);
    var dd2 = dt2.getDate();
    var mm2 = dt2.getMonth() + 1;
    var yy2 = dt2.getFullYear();

    if (dd1 == dd2 && mm1 == mm2 && yy1 == yy2)
        return true;
    else
        return false;
}


//function ConvertDateToString(curDt) {
//    var MM = curDt.getMonth() + 1;
//    MM = (MM < 10) ? '0' + MM : MM;
//    var dd = curDt.getDate();
//    dd = (dd < 10) ? '0' + dd : dd;
//    var yyyy = curDt.getFullYear();
//    var date = dd + '/' + MM + '/' + yyyy;

//    return date;
//}


// Dạng dd/MM/yyyy
function checkDateDDMMYYYY(dateString) {
 var marker = "/";
 var dateComp = dateString.split(marker);
    var now = new Date();
    //var yearNow = now.getFullYear();
    var dayInmonth = new Array(12);
    dayInmonth[0] = 31;
    dayInmonth[1] = 29;
    dayInmonth[2] = 31;
    dayInmonth[3] = 30;
    dayInmonth[4] = 31;
    dayInmonth[5] = 30;
    dayInmonth[6] = 31;
    dayInmonth[7] = 30;
    dayInmonth[8] = 31;
    dayInmonth[9] = 31;
    dayInmonth[10] = 30;
    dayInmonth[11] = 31;
    if (dateComp.length != 3) {
        //alert("Please enter correct date format for " + text + " (mm/dd/yyyy)!");
        return false;
    }
    for (var i = 0; i < 3; i++) {
        if (isNaN(dateComp[i])) {
            //alert("Please enter numeric for month, date, and year ( " + text + " )!");
            return false;
        }
    }
    if (dateComp[1] > 12 || dateComp[1] < 1) {
        //alert("Please enter a valid month for " + text + " (1 to 12)!");
        return false;
    }
//    if (dateComp[2] > yearNow + 1) {
//        //alert("Please enter a valid year for " + text + "! (future)");
//        return false;
//    }
//    if (dateComp[2] < yearNow - 1) {
//        //alert("Please enter a valid year for " + text + "! (past)");
//        return false;
//    }
    if (dateComp[2] % 4 == 0) {
        dayInmonth[1] = 29;
    }
    else {
        dayInmonth[1] = 28;
    }
    if (dateComp[0] > dayInmonth[dateComp[1] - 1] || dateComp[0] < 1) {
        //alert("Please enter a valid date!");
        return false;
    }

    return true;
}

function IsNumeric(sText) {
    var validChars = "0123456789/ ";
    var isNumber = true;
    var charr;
    for (var i = 0; i < sText.length && isNumber == true; i++) {
        charr = sText.charAt(i);
        if (validChars.indexOf(charr) == -1) {
            isNumber = false;
        }
    }
    return isNumber;
}

function ValidUserName(name) {
    name = name.replace(/(^\s*)|(\s*$)/g);
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if (name.match(alphaExp)) {
        return true;
    } else {
        return false;
    }
}

function IsEmpty(value) {
    var isEmpty = false;
    if (value.length == 0) {
        isEmpty = true;
    }
    return isEmpty;
}

function emailCheck(email) {
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        email.focus();
        return false;
    }
}


function Popup(actionPath) {
    var winl = (screen.width - 600) / 2;
    var wint = (screen.height - 400) / 2;
    var newWindow = null;
    var mybars = "directories=no,location=no,menubar=no,status=no";
    mybars += ",titlebar='',toolbar=no,alwaysraised=no,maximize=no";
    var myoptions = "scrollbars=yes,top=" + wint + ",left=" + winl + ",width=600px,height=380px,resizable=no";
    var myfeatures = mybars + "," + myoptions;
    if (!newWindow || newWindow.closed()) {
        newWindow = window.open(actionPath, "FloatPopUp", myfeatures);
        newWindow.focus();
    }
}
function PopupPageWithMenuBar(Url, Width, Height) {
    var OffsetHeight = document.body.offsetHeight;
    var OffsettWidth = document.body.offsetWidth;
    var objWindow = window.open(Url, "", "width=" + Width + ",height=" + Height + ",resizable=1,scrollbars=yes, menubar=1");
    objWindow.moveTo((OffsettWidth - Width) / 2, (OffsetHeight - Height) / 2);
}

//Chuyển kiểu ngày dạng "dd/MM/yy" sang "MM/dd/yy"

function ConvertddMMyyyToMMddyy(dateString) {
    try {
        var ddMMyy = new Array();
        ddMMyy = dateString.split('/');
        
        if (ddMMyy.length != 3)
            return false;
        
        var MMddyy = ddMMyy[1] + "/" + ddMMyy[0] + "/" + ddMMyy[2];
        return MMddyy;
        
    } catch(e) {
        return false;
    }
}

